<template>
  <div class="theme-container">
    <div class="cards-grid">
      <!-- 卡片数据 -->
      <div class="analysis-card">
        <h3 class="analysis-title">流量分析-卡片</h3>
        <div class="metrics-list">
          <div class="metric-item">
            <div class="metric-content">
              <div class="metric-values">
                <div class="metric-header">卡片曝光数</div>
                <div class="main-value">{{ getData('卡片曝光人数') }}次</div>
              </div>
            </div>
            <div style="display: flex; flex-direction: column; justify-content: space-between;">
              <div class="rate-value" :class="getData('卡片曝光人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                {{ getData('卡片曝光人数-较上周期变化') }}
              </div>
              <div class="previous-value">上期：{{ getPreviousValues('卡片曝光人数', '卡片曝光人数-较上周期变化') }}次</div>
            </div>
          </div>

          <div class="metric-item">
            <div class="metric-content">
              <div class="metric-values">
                <div class="metric-header">卡片成交金额</div>
                <div class="main-value">¥{{ getData('卡片成交金额') }}</div>
              </div>
            </div>
            <div style="display: flex; flex-direction: column; justify-content: space-between;">
              <div class="rate-value" :class="getData('卡片成交金额-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
                {{ getData('卡片成交金额-较上周期变化') }}
              </div>
              <div class="previous-value">上期：¥{{ getPreviousValues('卡片成交金额', '卡片成交金额-较上周期变化') }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="analysis-card">
        <h3 class="analysis-title">卡片曝光商品Top3</h3>
        <div class="top-products-list">
          <div v-for="(product, index) in getData('流量分析数据-卡片曝光商品TOP3')" :key="index" class="product-item">
            <div class="product-rank">
              <span class="rank-number">{{ index + 1 }}</span>
            </div>
            <div class="product-image-wrapper">
              <img :src="product.productImage" alt="商品图片" class="product-image" />
            </div>
            <div class="product-info">
              <div class="product-name">{{ product.productName }}</div>
              <div class="product-price">
                <span class="current-price">¥{{ product.productPrice?.toFixed(2) }}</span>
                <span class="original-price">¥{{ product.productOriginalPrice?.toFixed(2) }}</span>
              </div>
              <div class="exposure-count">曝光人数 {{ product.exposureCount }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getPreviousValue } from '../../../utils'
interface ReportItem {
  name: string;
  value: string;
}

interface ReportSection {
  title?: string;
  items: ReportItem[];
}

interface Props {
  sectionData: ReportSection
}

interface ProductItem {
  productName: string;
  productPrice: string;
  productOriginalPrice: string;
  productImage: string;
  exposureCount: string;
}

const props = defineProps<Props>()

const getPreviousValues = (currentKey: string, changeKey: string,): string => {

  const currentValue = getData(currentKey)
  const changeValue = getData(changeKey)
  return getPreviousValue(currentValue, changeValue)
}
function getData(key: '卡片曝光商品TOP3'): ProductItem[];
function getData(key: string): string;
function getData(key: string): any {
  if (!props.sectionData || !props.sectionData.items) {
    return key === '卡片曝光商品TOP3' ? [] : '-';
  }

  const item = props.sectionData.items.find((item: ReportItem) => item.name === key);
  // console.log('itemitemitemitem',item);

  if (key === '卡片曝光商品TOP3') {
    if (!item) return [];

    try {
      // 1. 替换 HTML 实体为正常字符
      const decodedString = item.value
        .replace(/&quot;/g, '"')
        .replace(/&amp;/g, '&')
        .replace(/&#39;/g, "'")
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>');

      // 2. 解析 JSON 字符串为对象
      return JSON.parse(decodedString) as ProductItem[];
    } catch (error) {
      console.error('解析商品数据失败:', error);
      return [];
    }
  }

  return item?.value || '-';
}


</script>

<style scoped>
.theme-container {
  margin-bottom: 20px;
  width: 100%;
  /* 确保整个容器不会被分页 */
  break-inside: avoid-page !important;
  page-break-inside: avoid !important;
  display: block;
  /* 确保容器是块级元素 */
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  /* 确保grid容器也不会被分页 */
  break-inside: avoid-page !important;
  page-break-inside: avoid !important;
}

.analysis-card {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 15px;
  /* 确保卡片不会被分页 */
  break-inside: avoid-page !important;
  page-break-inside: avoid !important;
}

.analysis-title {
  font-size: 18px;
  color: #2d3748;
  font-weight: 600;
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 2px solid #e2e8f0;
}

.metrics-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.metrics-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.metric-item {
  background: #f8fafc;
  border-radius: 8px;
  padding: 12px;
  display: flex;
  justify-content: space-between;
  /* 移除margin，因为使用了grid的gap */
  margin: 0;
}

.metric-header {
  color: #4a5568;
  font-size: 13px;
  margin-bottom: 6px;
  text-align: left;
}

.metric-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.metric-values {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.main-value {
  font-size: 20px;
  font-weight: 600;
  color: #2d3748;
  line-height: 1;
}

.previous-value {
  font-size: 13px;
  color: #718096;
  text-align: right;
}

.rate-value {
  font-size: 13px;
  padding: 2px 8px;
  border-radius: 4px;
  line-height: 1.2;
  text-align: center;
}

.rate-up {
  color: #22c55e;
  background: rgba(34, 197, 94, 0.1);
}

.rate-down {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

.top-products-list {
  padding: 5px 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.product-item {
  display: flex;
  align-items: center;
  padding: 8px;
  border-radius: 8px;
  background: #f8fafc;
}

.product-rank {
  margin-right: 10px;
  width: 26px;
  text-align: center;
}

.rank-number {
  display: inline-block;
  width: 26px;
  height: 26px;
  line-height: 26px;
  text-align: center;
  color: white;
  font-size: 14px;
  font-weight: bold;
  background-color: #4A5568;
  border-radius: 50%;
}

.product-item:nth-child(1) .rank-number {
  background-color: #FFB800;
}

.product-item:nth-child(2) .rank-number {
  background-color: #B4B4B4;
}

.product-item:nth-child(3) .rank-number {
  background-color: #CD7F32;
}

.product-image-wrapper {
  width: 60px;
  height: 60px;
  margin-right: 10px;
  overflow: hidden;
  border-radius: 6px;
}

.product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
}

.product-name {
  font-size: 14px;
  color: #2d3748;
  font-weight: 500;
  margin-bottom: 5px;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-price {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 5px;
}

.current-price {
  font-size: 16px;
  font-weight: 600;
  color: #2d3748;
}

.original-price {
  font-size: 13px;
  color: #a0aec0;
  text-decoration: line-through;
}

.exposure-count {
  font-size: 13px;
  color: #718096;
  text-align: right;
}
</style>